package org.freedoit.server.dao;

// Generated Oct 11, 2012 3:54:06 PM by Hibernate Tools 3.4.0.CR1

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import org.freedoit.server.om.TaskType;
import org.freedoit.shared.exception.ServerException;

/**
 * Home object for domain model class TaskType.
 * @see org.freedoit.om.TaskType
 * @author Hibernate Tools
 */
public class TaskTypeDAO {

	//private static final Log log = LogFactory.getLog(TaskTypeDAO.class);

	private final EntityManagerFactory emFactory = DAOFactory.getEntityManagerFactory();

	public void persist(TaskType taskType) {
		//log.debug("persisting TaskType instance");
		EntityManager em = emFactory.createEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(taskType);

			em.getTransaction().commit();
			//log.debug("persist successful");
		} catch (RuntimeException re) {
			//log.error("persist failed", re);
			throw new ServerException(re);
		} finally {
			em.close();
		}
	}

	public void update(TaskType taskType) {
		//log.debug("updating TaskType instance");
		EntityManager em = emFactory.createEntityManager();
		try {
			em.getTransaction().begin();
			em.merge(taskType);
			em.getTransaction().commit();
			//log.debug("update successful");
		} catch (RuntimeException re) {
			//log.error("update failed", re);
			throw new ServerException(re);
		} finally {
			em.close();
		}
	}

	public TaskType findById(Long id) {
		//log.debug("getting TaskType instance with id: " + id);
		EntityManager em = emFactory.createEntityManager();
		try {
			TaskType instance = em.find(TaskType.class, id);
			if (instance == null) {
				//log.debug("get successful, no instance found");
			} else {
				//log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			//log.error("get failed", re);
			throw new ServerException(re);
		} finally {
			em.close();
		}
	}

	public List<TaskType> findAll() {
		//log.debug("getting TaskType instance with id: " + id);
		EntityManager em = emFactory.createEntityManager();
		try {
			StringBuffer sbQuery = new StringBuffer();
			sbQuery.append("from ").append(TaskType.class.getName());
			List<TaskType> results = em.createQuery(sbQuery.toString(), TaskType.class).getResultList();
			
//			int iSize = 0;
//			if (results != null) {
//				iSize = results.size();
//			}
			//log.debug("find by user id successful, result size: " + iSize);
			return results;
		} catch (RuntimeException re) {
			//log.error("get failed", re);
			throw new ServerException(re);
		} finally {
			em.close();
		}
	}
}
